package com.commerce.addressbook.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

import androidx.recyclerview.widget.RecyclerView;

import com.commerce.addressbook.Entity.DataBean;
import com.commerce.addressbook.R;
import com.commerce.addressbook.activity.ContactsActivity;

import java.util.ArrayList;
import java.util.List;

public class MyListViewAdapter extends BaseAdapter {

    private List<DataBean> list;

    private int flg = 1;
    private int positionFlg;

    public MyListViewAdapter(List<DataBean> list) {
        this.list=list;
    }

    public List<DataBean> getList(){
        return list;
    }

    @Override
    public int getCount() {
        return list.size()+flg;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(final int position, View view, ViewGroup parent) {
          final ViewHolder viewHolder;
          if(view==null){
             view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_phone,null);
             viewHolder=new ViewHolder();
             viewHolder.editText = view.findViewById(R.id.dh);
             view.setTag(viewHolder);
          }else {
             viewHolder = (ViewHolder) view.getTag();
          }
        String phone = "";
          if(list!=null&&list.size()> position){
              phone = list.get(position).getPhone();
              viewHolder.editText.setText(phone);
          }
        if(positionFlg==position){
            viewHolder.editText.requestFocus();
            viewHolder.editText.setSelection(phone.length());
        }
        viewHolder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s){
                //是否刷新标志
                 int flgClone = flg+list.size();
                 //字符串改动监听
                if(s.toString().length()>0){
                    if(list!=null&&list.size()> position){
                        if(!list.get(position).getPhone().equals(s.toString())){
                            flg = 1;
                        }
                        list.get(position).setPhone(s.toString());
                    }else if (list.size()>0) {
                        flg = 1;
                        list.add(new DataBean(list.get(0).getIds(),s.toString()));
                    }else {
                        list.add(new DataBean("-1",s.toString()));
                    }
                }else if(s.toString().length()==0 && list.size()== position +1) {
                    list.remove(position);
                    flg = 1;
                }else if(s.toString().length()==0&&list!=null&&list.size()> position){
                    list.get(position).setPhone("");
                    flg = 0;
                }
                //刷新
                if(flgClone!=flg+list.size() ){
                    //光标标记
                    positionFlg = position;
                    notifyDataSetChanged();
                }

            }

        });

        return view;
    }


    private class ViewHolder{
        EditText editText;
    }
}
